package com.jiulan.hearthearing.ui.my.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jiulan.hearthearing.R;
import com.jiulan.hearthearing.base.BaseActivity;
import com.jiulan.hearthearing.constants.Constants;
import com.jiulan.hearthearing.constants.UrlConstants;
import com.jiulan.hearthearing.http.HttpRequest;
import com.jiulan.hearthearing.http.HttpStringCallBack;
import com.jiulan.hearthearing.ui.my.adapter.MyCourseAdapter;
import com.jiulan.hearthearing.ui.my.model.MyCourseModle;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 我的学习
 */
public class MyCourseActivity extends BaseActivity {
    private int page = 1;
    private MyCourseAdapter myCourseAdapter;
    private List<MyCourseModle.DataBean> data;

    private SmartRefreshLayout smartLayout;
    private RecyclerView mRecyclerView;

    public static void actionStart(Context context) {
        Intent intent = new Intent(context, MyCourseActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void init(Bundle savedInstanceState) {

    }

    @Override
    protected int setLayoutResourceID() {
        return R.layout.layout_smart_refresh;
    }

    @Override
    protected void initView() {
        setTitleText("我的学习");
        smartLayout = findViewById(R.id.smartLayout);
        mRecyclerView = findViewById(R.id.mRecyclerView);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        setView();
    }

    private void setView() {
        data = new ArrayList<>();
        smartLayout.setEnableAutoLoadMore(true);//开启自动加载功能（非必须）
        smartLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page = 1;
                        getData();
                    }
                }, 1000);
            }
        });
        smartLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                refreshLayout.getLayout().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        page++;
                        getData();
                    }
                }, 1000);
            }
        });
    }

    private void getData() {
        Map<String, String> map = new HashMap();
        map.put("page", String.valueOf(page));//分页
        map.put("limit", String.valueOf(Constants.LIMIT));//分页
        new HttpRequest(getContext()).doPost(UrlConstants.course_need_study, null, map, MyCourseModle.class, new HttpStringCallBack() {
            @Override
            public void onSuccess(Object result) {
                if (result instanceof MyCourseModle) {
                    MyCourseModle model = (MyCourseModle) result;
                    if (page == 1) {
                        data.clear();
                        if (model.getData().size() > 0) {
//                            no_data.setVisibility(View.GONE);
                            data = model.getData();
                            myCourseAdapter = new MyCourseAdapter(data);
                            mRecyclerView.setAdapter(myCourseAdapter);
                            if (model.getData().size() == Constants.LIMIT) {
                                //还有多的数据
                                smartLayout.finishRefresh();
                            } else {
                                //没有更多数据（上拉加载功能将显示没有更多数据）
                                smartLayout.finishRefreshWithNoMoreData();
                            }
                            myCourseAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
                                @Override
                                public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                                    switch (view.getId()) {
                                        case R.id.tvStudy:
                                            break;
                                    }
                                }
                            });
                        } else {
                            smartLayout.finishRefreshWithNoMoreData();
//                            no_data.setVisibility(View.VISIBLE);
                        }
                    } else {
                        data.addAll(model.getData());
                        myCourseAdapter.notifyDataSetChanged();
                        if (model.getData().size() == Constants.LIMIT) {
                            //还有多的数据
                            smartLayout.finishLoadMore();
                        } else {
                            //没有更多数据（上拉加载功能将显示没有更多数据）
                            smartLayout.finishLoadMoreWithNoMoreData();//将不会再次触发加载更多事件
                        }
                    }
                }
            }

            @Override
            public void onFailure(int code, String msg) {

            }
        });
    }

    @Override
    protected void initData() {
        smartLayout.autoRefresh();
    }

    @Override
    protected int getTitleBarType() {
        return TITLEBAR_DEFAULT;
    }
}
